package redis

import (
	"fmt"

	"github.com/e421083458/golang_common/lib"
	"github.com/garyburd/redigo/redis"
)

var Redis_conn redis.Conn

func init() {
	fmt.Println("连接redis")
	redis_conn, err := lib.RedisConnFactory("default")
	// redis_conn, err := redis.Dial("tcp", "127.0.0.1:6379")

	if err != nil {
		fmt.Println("Redis连接失败", err.Error())
	}
	Redis_conn = redis_conn
	// defer redis_conn.Close()

}

func Set(key string, val string, expire int) (interface{}, error) {
	replyval, err := Redis_conn.Do("Set", key, val)
	if expire > 0 {
		replyval, err = Redis_conn.Do("SETEX", key, expire, val)
	}
	return replyval, err
}

func Get(key string) string {
	val, err := redis.String(Redis_conn.Do("Get", key))
	if err != nil {
		return ""
	}
	return val
}

func Del(key string) bool {
	_, err := Redis_conn.Do("Del", key)
	if err != nil {
		return false
	}
	return true
}
